package com.bix.util.blizfiles.mpq;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * This class encapsulates a MPQ file and all associated functionality.
 * 
 * @author squid
 */
public class	MPQ {
	private	long			archiveOffset;
	
	private	MPQHeader	header;
	
	public MPQ (String filename) throws IOException, MPQException {
		File						file = new File (filename);
		FileInputStream	fis = new FileInputStream (file);
		FileChannel			fc = fis.getChannel();

		this.header = new MPQHeader ();
		
		//
		// We are going to look for the header in the file that was passed to us.
		// The header MAY NOT be the very first thing in the file, however, it is
		// guaranteed to be on a 512 byte boundary.  So we start at the beginning
		// of the file and continue looking for the header starting at offsets of
		// 512 bytes.  If the header is not found, an IOException will be thrown.
		//
		this.archiveOffset = 0;
		while (header.read (fc) == true) {
			if (header.isValid()) {
				long offset = this.header.getHashTableOffset() + (this.header.getHashTableOffsetHigh() << 32) + this.archiveOffset;
				fc.position(offset);

				MPQHashTable ht = new MPQHashTable ();
				ht.read(fc);

				break;
			}
			this.archiveOffset += 512;
			fc.position(this.archiveOffset);
		}
		
		fis.close();
	}
	
	public boolean isValid () {
		return this.header.isValid();
	}

	public String toString () {
		String	s = "";
		
		s += "Header[" + this.header + "]";
		
		return s;
	}
}